import pandas as pd
import os
import re
import datetime
import time
nowtime = str()
now = datetime.datetime.now()
nowtime = str(now.year) + "-" + str(now.month) + "-" + str(now.day) + "-" + str(now.hour) + "-" + str(now.minute) + "-" + str(now.second)

def XinHaoQuery(LotNumber):
    # df.iloc方法，根据行、列的数字位置查询
    database = r"./database/freq.xlsx"
    #工序号	工序名称	设备名称
    df = pd.read_excel(database,dtype={'批次号': str,'型号': str,'姓名':str,'工序号':str,'工序名称':str,'设备名称':str})
    # order_type=df.loc[dfxinhao, "型号"]
    order_type=df.loc[df['批次号'] == LotNumber, '型号'].values[0]
    name = df.loc[df['批次号'] == LotNumber, '姓名'].values[0]
    processid=df.loc[df['批次号'] == LotNumber, '工序号'].values[0]
    processname=df.loc[df['批次号'] == LotNumber, '工序名称'].values[0]
    devicename=df.loc[df['批次号'] == LotNumber, '设备名称'].values[0]

    # print(order_type)
    # print(df)
    # a=df.loc[0, '批次号']
    # print(a)
    # print(type(a))
    return order_type,name,processid,processname,devicename
LotNumber=str('SJ23050865-SZ')
ProductType,Name,ProcessID,ProcessName,DeviceName=XinHaoQuery(LotNumber)

path = r"./needrename"
patter=re.compile(r'(\d*-\d*)\.xls')

for file in os.listdir(path):
    if file.endswith(".xls"):
        file_result=re.search(patter,file)
        if file_result:
            print(file_result.group(1))
            # print(XinHaoQuery(file_result.group(1)))
            ModifiedTime=os.path.getmtime(os.path.join(path,file))#输出文件最近修改时间
            time_tuple = time.localtime(ModifiedTime)
            y = time.strftime('%Y', time_tuple)
            m = time.strftime('%m', time_tuple)
            d = time.strftime('%d', time_tuple)
            H = time.strftime('%H', time_tuple)
            M = time.strftime('%M', time_tuple)
            S = time.strftime('%S', time_tuple)
            print(y,m,d,H,M,S)
            ModifiedTimeName=y+"-"+m+"-"+d+"-"+H+"-"+M+"-"+S

            os.rename(os.path.join(path,file),
                      os.path.join(path,
                                   LotNumber + "_"
                                   + ProductType + "_"
                                   +ProcessID+"_"
                                   +ProcessName+"_"
                                   +DeviceName+"_"
                                   +file_result.group(1)+"_"+Name+"_"
                      +ModifiedTimeName+".xls"))


